/**
 * Created by zycool on 2016/9/6.
 * 涂层维修 - 维修整改 - 腐蚀控制
 */

;(function ($, window, document, undefined) {

    /*** 新建 - start ***/
    // 项目ID
    var $id = $('#JQ_repairTaskId');
    // 项目名称
    var $name = $('#JQ_name');
    // 施工单位
    var $assemblyCompany = $('#JQ_assemblyCompany');
    // 开工时间
    var $planStartDate = $('#JQ_planStartDate');
    // 峻工时间
    var $planCompletionDate = $('#JQ_planCompletionDate');
    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段'
    });
    // 起点
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        param: {
            'pipeLineId': $pipeline,
            'pipeSegmentId': $segment
        }
    });
    // 终点
    var $end = $('#JQ_end').select3({
        title: '终点位置',
        server: true,
        param: {
            'pipeLineId': $pipeline,
            'pipeSegmentId': $segment,
            'beginSignId': $begin
        }
    });
    // 触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $segment.add([]);
            $begin.add([]);
            $end.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $begin.add([]);
            $end.add([]);
        }
    });
    /*** 新建 - end ***/

    /*** 筛选 - start ***/
    // 管道
    var $pipeline_exist = $('#JQ_pipeline_exist').select3({
        data: $pipeline.val('store')
    });
    // 管段
    var $segment_exist = $('#JQ_segment_exist').select3({
        title: '管段'
    });
    // 起点
    var $begin_exist = $('#JQ_begin_exist').select3({
        title: '起点位置',
        server: true,
        param: {'pipeLineId': $pipeline_exist, 'pipeSegmentId': $segment_exist}
    });
    // 终点
    var $end_exist = $('#JQ_end_exist').select3({
        title: '终点位置',
        server: true,
        param: {'pipeLineId': $pipeline_exist, 'pipeSegmentId': $segment_exist, 'beginSignId': $begin_exist}
    });
    // 触发管道
    $pipeline_exist.element.on('select', function (e, v) {
        var id = v || $pipeline_exist.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment_exist.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin_exist.asset(begin);
                $end_exist.asset(end);
            });
        } else {
            $segment_exist.add([]);
            $begin_exist.add([]);
            $end_exist.add([]);
        }
    });
    // 触发管段
    $segment_exist.element.on('select', function (e, v) {
        var id = v || $segment_exist.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin_exist.asset(begin);
                $end_exist.asset(end);
            });
        } else {
            $begin_exist.add([]);
            $end_exist.add([]);
        }
    });
    // 修复建议
    var $advise_exist = $('#JQ_advise_exist').select3();
    // 建议修复时间 - 1
    var $planStartDate1 = $('#JQ_planStartDate1');
    // 建议修复时间 - 2
    var $planStartDate2 = $('#JQ_planStartDate2');
    // 已存在漏损点列表
    var $damage_table_exist = $('#JQ_damage_table_exist').checkbox3();
    // 已选择漏损点列表
    var $damage_table_select = $('#JQ_damage_table_select').checkbox3();
    /*** 筛选 - end ***/


    // 隐藏的数组中间件
    var $damage_ids = $('#JQ_damage_ids');
    // 工作量
    var $work = $('#JQ_coatRepair');
    // 筛选 & 新建
    var filter = {
        // 新建
        getCreateRepair: function (tag, param) {
            var data = {};
            data.mode = true;
            // 分页参数
            if(param){
                data['jsonBean.pageNum'] = 1;
                data['jsonBean.pageCount'] = 9999;
            }
            // ID
            data.id = function () {
                return $id.val();
            }();
            // 项目名称
            data.name = function () {
                var v = $name.val();
                if (v) {
                    return v;
                } else {
                    $name.TIP({content: '项目名称不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            // 施工单位
            data.assemblyCompany = function () {
                var v = $assemblyCompany.val();
                if (v) {
                    return v;
                } else {
                    $assemblyCompany.TIP({content: '施工单位不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            data.planStartDate = function () {
                var v = $planStartDate.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.planCompletionDate = function () {
                var v1 = $planStartDate.val();
                var v2 = $planCompletionDate.val();
                if (v2) {
                    if(v1){
                        if (!(Dyy.checkDateLate(v1, v2))) {
                            $planCompletionDate.TIP({content: '竣工时间不能早于开工时间!'});
                            data.mode = false;
                        }
                    }
                    return v2;
                }
                return '';
            }();
            data.pipeLineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                } else {
                    $pipeline.element.TIP({content: '管道名称不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            data.fromPointId = function () {
                var v = $begin.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.fromPointName = function () {
                var v = $begin.val('text');
                if (v) {
                    return v;
                }
                return '';
            }();
            data.beginMileage = function () {
                var v = $begin.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            }();
            data.toPointId = function () {
                var v = $end.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.toPointName = function () {
                var v = $end.val('text');
                if (v) {
                    return v;
                }
                return '';
            }();
            data.endMileage = function () {
                var v = $end.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            }();
            data.defectInfoId = function () {
                return filter.getChangeIds();
            }();
            data.dataStated = function () {
                if (tag === 'save' || tag === 'create' || tag === 'edit') {
                    return '01';
                } else if (tag === 'release') {
                    return '20';
                }
            }();
            data.workLoad = function () {
                var v = $work.val();
                if (isNaN(v)) {
                    $work.TIP({
                        content: '工作量须为数字!',
                        scroll: false
                    });
                    data.mode = false;
                } else {
                    return v;
                }
                return '';
            }();

            if (tag === 'create' || tag === 'edit') {
                return data;
            } else if (tag === 'save' || tag === 'release') {
                return DyyBase.convertToArr(data);
            }
        },
        // 查询已存在
        getExistRepair: function () {
            var data = {};
            data.mode = true;
            data.includeRepairTaskId = function(){
                var v = $id.val();
                if(v){
                    return v;
                }
                return '';
            }();
            data.pipeId = function () {
                var v = $pipeline_exist.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.beginMileage = function () {
                var v = $begin_exist.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            }();
            data.endMileage = function () {
                var v = $end_exist.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            }();
            data.latestBeginTime = function () {
                var v = $planStartDate1.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.latestEndTime = function () {
                var v = $planStartDate2.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.repairSuggestion = function () {
                var v = $advise_exist.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.excludeDeIds = function () {
                var arr = [];
                var html = $('#JQ_damage_ids').html();
                if (html) {
                    arr = eval('(' + html + ')');
                }
                return arr;

            }();
            return DyyBase.convertToArr(data);
        },
        // 获取改变后的漏损点ID数组参数
        getChangeIds: function () {
            var arr = [];
            var html = $damage_ids.html();
            if (html) {
                arr = eval('(' + html + ')');
            }
            return arr;
        },
        // 获取已存在漏损点列表选中id
        getExistIds: function () {
            var $chk = $damage_table_exist.find('i.zy-checkbox');
            var arr = [];
            if ($chk.length > 0) {
                $chk.each(function () {
                    var $this = $(this);
                    if ($this.hasClass('active')) {
                        arr.push($this.attr('data-id'));
                    }
                });
            }
            return arr;
        },
        // 获取已选择漏损点列表选中id
        getSelectIds: function (tag) {
            var $chk = $damage_table_select.find('i.zy-checkbox');
            var arr = [];
            if ($chk.length > 0) {
                $chk.each(function () {
                    var $this = $(this);
                    if (tag == 'active') {
                        if ($this.hasClass('active')) {
                            arr.push($this.attr('data-id'));
                        }
                    } else if (tag == 'all') {
                        arr.push($this.attr('data-id'));
                    }
                });
            }
            return arr;
        },
        // 添加div漏损点ID数组参数
        addChangeIds: function (a, callback) {
            var html = $damage_ids.html();
            var arr = [];
            if (html) {
                arr = eval('(' + html + ')');
            }
            if (a.length > 0) {
                arr = arr.concat(a);
            }
            var str = '';
            if (arr.length > 0) {
                for (var i = 0, len = arr.length; i < len; i++) {
                    if (i == len - 1) {
                        str += '"' + arr[i] + '"';
                    } else {
                        str += '"' + arr[i] + '",';
                    }
                }
            }
            str = '[' + str + ']';
            $damage_ids.empty().html(str);

            if(callback){
                callback();
            }
        },
        // 删除div漏损点ID数组参数
        deleteChangeIds: function (a, callback) {
            var html = $damage_ids.html();
            var arr = [];
            if (html) {
                arr = eval('(' + html + ')');
            }
            DyyBase.deleteArray(arr,a);
            var str = '';
            if (arr.length > 0) {
                for (var k = 0, lenth = arr.length; k < lenth; k++) {
                    if (k == lenth - 1) {
                        str += '"' + arr[k] + '"';
                    } else {
                        str += '"' + arr[k] + '",';
                    }
                }
            }
            str = '[' + str + ']';
            $damage_ids.empty().html(str);

            if(callback){
                callback();
            }
        }
    };

    /**
     * 初始post数据项 - start
     */
    window.initData = {
        // 维修任务ID
        id: function(){
            var v = $id.val();
            if(v){
                return v;
            }
            return '';
        }(),
        // 维修任务名称
        name: '',
        // 施工单位
        assemblyCompany: '',
        // 管道ID
        pipeLineId: function(){
            var v = $('#JQ_pipeId').val();
            if(v){
                return v;
            }
            return '';
        }(),
        // 管道名称
        pipeLineName: function(){
            var v = $('#JQ_pipeName').val();
            if(v){
                return v;
            }
            return '';
        }(),
        // 起点ID
        fromPointId: function(){
            var v = $('#JQ_startId').val();
            if(v){
                return v;
            }
            return '';
        }(),
        // 起点名称
        fromPointName: function(){
            var v = $('#JQ_startName').val();
            if(v){
                return v;
            }
            return '';
        }(),
        // 起点距离
        fromPointMileage: function(){
            var v = $('#JQ_startMile').val();
            if(v){
                return v;
            }
            return '';
        }(),
        // 终点ID
        toPointId: function(){
            var v = $('#JQ_endId').val();
            if(v){
                return v;
            }
            return '';
        }(),
        // 终点名称
        toPointName: function(){
            var v = $('#JQ_endName').val();
            if(v){
                return v;
            }
            return '';
        }(),
        // 终点ID
        toPointMileage: function(){
            var v = $('#JQ_endMile').val();
            if(v){
                return v;
            }
            return '';
        }(),
        // 开工时间
        planStartDate: '',
        // 竣工时间
        planCompletionDate: '',
        // 20发布、01保存
        dataStated: '',
        // 缺陷点编号 - 自身存取 - 来自一键维修提交 - 来自编辑
        defectInfoId: function(){
            return filter.getChangeIds();
        }(),
        // 工作量
        workLoad: ''
    };
    /**
     * 初始post数据项 - end
     */


    // 漏损点列表(已存在/已选择)
    var DTEXIST, DTSELECT;

    // 加载已选择的漏损点
    function loadSelectDamage() {
        DTSELECT = $damage_table_select.DataTable({
            'language': {
                'zeroRecords': DyyBase.table_empty(),
                'infoEmpty': ''
            },
            'paging': false,
            'info': false,
            'autoWidth': false,
            'order': [],
            'serverSide': true,
            'pageLength': 9999,
            'ordering': false,
            'processing': false,
            'searching': false,
            'lengthChange': false,
            'ajax': function (data, callback) {
                var param = filter.getCreateRepair('create',data);
                DyyBase.ajax(corrctrlApi.url_paint_details(), DyyBase.convertToArr(param), function (dataset) {
                    var returnData = {};
                    returnData.draw = data.draw;
                    returnData.recordsTotal = 9999;
                    returnData.recordsFiltered = 9999;
                    returnData.data = DyyBase.clearNull(dataset.result.defectInfoList.list);
                    callback(returnData);
                });
            },
            'drawCallback': function () {
                $damage_table_select.find('i.zy-checkbox-all').trigger('clear');
                $work.trigger('turn');
            },
            'columns': [
                {
                    data: 'id', render: function (data) {
                    return '<i class="zy-checkbox" data-id="' + data + '" title="选中"></i>';
                }
                },
                {
                    data: 'pipeLineName', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'exinspTaskName', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'customNumber', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'nagmCustomNumber', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'dbValue', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'buriedDepth', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'faultGrade', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'repairSuggestion_label', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'latestRepairTime', render: function (data) {
                    var time = Dyy.getDate(data);
                    return '<span title="' + time + '">' + time + '</span>';
                }
                },
                {
                    data: 'id', render: function (data) {
                    return '<i title="展开详情" class="zy-details-icon" data-id="' + data + '"></i>';
                }
                }
            ]
        });
    }

    // 加载已存在的漏损点
    function loadExistDamage() {
        DTEXIST = $damage_table_exist.DataTable({
            'language': {
                'zeroRecords': DyyBase.table_empty(),
                'infoEmpty': ''
            },
            'paging': false,
            'info': false,
            'autoWidth': false,
            'order': [],
            'serverSide': true,
            'pageLength': 1000,
            'ordering': false,
            'processing': false,
            'searching': false,
            'lengthChange': false,
            'ajax': function (data, callback) {
                var param = filter.getExistRepair();
                DyyBase.ajax(corrctrlApi.url_paint_exist(), param, function (dataset) {
                    var returnData = {};
                    returnData.draw = data.draw;
                    returnData.recordsTotal = 1000;
                    returnData.recordsFiltered = 1000;
                    returnData.data = DyyBase.clearNull(dataset.result);
                    callback(returnData);
                });
            },
            'drawCallback': function () {
                $damage_table_exist.find('i.zy-checkbox-all').trigger('clear');
            },
            'columns': [
                {
                    data: 'id', render: function (data) {
                    return '<i class="zy-checkbox" data-id="' + data + '" title="选中"></i>';
                }
                },
                {
                    data: 'pipeLineName', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'exinspTaskName', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'customNumber', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'nagmCustomNumber', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'dbValue', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'buriedDepth', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'faultGrade', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'repairSuggestion_label', render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
                },
                {
                    data: 'latestRepairTime', render: function (data) {
                    var time = Dyy.getDate(data);
                    return '<span title="' + time + '">' + time + '</span>';
                }
                },
                {
                    data: 'id', render: function (data) {
                    return '<i title="展开详情" class="zy-details-icon" data-id="' + data + '"></i>';
                }
                }
            ]
        });
        // 修复建议
        DyyBase.ajax(DyyBase.url_repair_suggestion(), {}, function (dataset) {
            var json = DyyBase.getSelect(dataset);
            $advise_exist.add(json);
        });
    }

    // 添加更多漏损点
    $('#JQ_add_more').click(function () {
        var $this = $(this);
        $this.toggleClass('active');
        $('#JQ_load_exist').slideToggle('fast');
        if ($this.attr('data-status') == '0') {
            if(!DTEXIST){
                loadExistDamage();
            }else{
                DTEXIST.draw();
            }
            $this.attr('data-status', '1');
        }
    });

    // 新建 or 编辑 - 下一步
    $('#JQ_go_next').click(function () {
        var param = filter.getCreateRepair('create');
        if (param.mode) {
            $('#JQ_step_01').hide();
            $('#JQ_step_02').show();
            if(!DTSELECT){
                loadSelectDamage();
            }else{
                DTSELECT.draw();
            }
            // 填充筛选条件
            $pipeline_exist.fit(param.pipeLineId).disable();
            $segment_exist.disable();
            $begin_exist.asset([{id:param.fromPointId,text:param.fromPointName,mile:param.beginMileage}]).disable();
            $end_exist.asset([{id:param.toPointId,text:param.toPointName,mile:param.endMileage}]).disable();
        }
    });
    // 返回 - 列表
    $('#JQ_go_list').click(function () {
        window.location.href = DyyBase.root_path + '/corrctrl/CoatRepairTask/init.html';
    });

    // 查询已存在
    $('#JQ_search_exist').click(function () {
        if (DTEXIST) {
            DTEXIST.draw();
        }
    });

    // 涂层维修详情模板
    var repairTemplate = [
        {name: 'pipeLineName', title: '管道名称：'},
        {name: 'customNumber', title: '漏损点编号：'},
        {name: 'buriedDepth', title: '埋深(m)：'},
        {name: 'dbValue', title: 'dB值：'},
        {name: 'irValue', title: 'IR%：'},
        {name: 'onPotential', title: '通电电位(mv)：'},
        {name: 'offPotential', title: '断电电位(mv)：'},
        {name: 'faultGrade', title: '漏损点等级：'},
        {name: 'repairSuggestion_label', title: '修复建议：'},
        {
            name: 'latestRepairTime', title: '最迟维修时间：', render: function (d) {
            return Dyy.getDate(d);
        }
        },
        {name: 'repairState_label', title: '修复状态：'},
        {
            name: 'testingDate', title: '检测日期：', render: function (d) {
            return Dyy.getDate(d);
        }
        },
        {name: 'testingCompany', title: '检测单位：'},
        {
            name: 'recordTime', title: '记录时间：', render: function (d) {
            return Dyy.getDate(d);
        }
        },
        {name: 'recorder', title: '记录人：'},
        {name: 'mileage', title: '绝对距离(m)：', render: function(d){
            return (parseFloat(d)/1000);
        }},
        {name: 'nagmCustomNumber', title: '最近测试桩：'},
        {name: 'nagmMileage', title: '相对最近测试桩距离(m)：', render: function(d){
            return (parseFloat(d)/1000);
        }},
        {name: 'locationDescription', title: '位置描述：'},
        {name: 'acInterference_label', title: '交流干扰：'},
        {name: 'dcInterference_label', title: '直流干扰：'},
        {name: 'remarks', title: '备注：'}
    ];

    // 工作量
    $work.on('turn', function () {
        var $this = $(this);
        var html = $damage_ids.html();
        var arr = [];
        if (html) {
            arr = eval('(' + html + ')');
        }
        var length = arr.length;
        $this.val(length);
    });
    $work.on('blur', function () {
        var $this = $(this);
        var v = $this.val();
        if (isNaN(v)) {
            $this.TIP({
                content: '工作量须为数字!',
                scroll: false
            });
        }
    });

    // 已存在列表展开详情
    $damage_table_exist.on('click', 'i.zy-details-icon', function () {
        DyyBase.showDetails($(this), DTEXIST, $damage_table_exist, repairTemplate);
    });

    // 已选择列表展开详情
    $damage_table_select.on('click', 'i.zy-details-icon', function () {
        DyyBase.showDetails($(this), DTSELECT, $damage_table_select, repairTemplate);
    });

    // 添加漏损点
    $('#JQ_exist_add').click(function () {
        var $this = $(this);
        var arr = filter.getExistIds();
        if (arr.length > 0) {
            filter.addChangeIds(arr, function () {
                if (DTEXIST) {
                    DTEXIST.draw();
                }
                if (DTSELECT) {
                    DTSELECT.draw();
                } else {
                    loadSelectDamage();
                }
            });
        } else {
            $this.MSG({
                content: '请选择要添加的漏损点!'
            });
        }
    });

    // 删除已选择的漏损点
    $('#JQ_select_delete').click(function () {
        var $this = $(this);
        var arr = filter.getSelectIds('active');
        if (arr.length > 0) {
            filter.deleteChangeIds(arr, function () {
                if (DTEXIST) {
                    DTEXIST.draw();
                }
                if (DTSELECT) {
                    DTSELECT.draw();
                }
            });
        } else {
            $this.MSG({
                content: '请选择要删除的漏损点!'
            });
        }
    });

    // 返回上一步
    $('#JQ_go_prev').click(function () {
        $('#JQ_step_01').show();
        $('#JQ_step_02').hide();
        $('#JQ_add_more').attr('data-status', '0').removeClass('active');
        $('#JQ_load_exist').hide();
    });

    // 保存
    $('#JQ_save').click(function () {
        var $this = $(this);
        var param = filter.getCreateRepair('save');
        if (param[0].value) {
            DyyBase.ajax(corrctrlApi.url_paint_create(), param, function (dataset) {
                if (dataset.bool) {
                    $this.MSG({
                        type: 'success',
                        content: '保存涂层维修项目成功!',
                        yesBtnAct: DyyBase.goToPage,
                        closeBtnAct: DyyBase.goToPage,
                        goToUrl: DyyBase.root_path + '/corrctrl/CoatRepairTask/init.html'
                    });
                } else {
                    $this.MSG({
                        type: 'error',
                        content: dataset.result
                    });
                }
            });
        }
    });

    // 发布
    $('#JQ_release').click(function () {
        var $this = $(this);
        var param = filter.getCreateRepair('release');
        if (param[0].value) {
            DyyBase.ajax(corrctrlApi.url_paint_create(), param, function (dataset) {
                if (dataset.bool) {
                    $this.MSG({
                        type: 'success',
                        content: '发布涂层维修项目成功!',
                        yesBtnAct: DyyBase.goToPage,
                        closeBtnAct: DyyBase.goToPage,
                        goToUrl: DyyBase.root_path + '/corrctrl/CoatRepairTask/init.html'
                    });
                } else {
                    $this.MSG({
                        type: 'error',
                        content: dataset.result
                    });
                }
            });
        }
    });

    /*** 编辑 - start ***/
    if (window.initData.id) {
        DyyBase.ajax(corrctrlApi.url_paint_details(), {repairTaskId: $id.val()}, function(dataset){
            if(dataset.bool){
                var task = dataset.result.repairTask;
                var defect = dataset.result.defectInfoList;
                if(task){
                    // 基本信息
                    $name.val(task.name);
                    $assemblyCompany.val(task.assemblyCompany);
                    $planStartDate.val(Dyy.getDate(task.planStartDate,true));
                    $planCompletionDate.val(Dyy.getDate(task.planCompletionDate,true));
                    $pipeline.fit(task.pipeLineId).disable();

                    // 管段
                    DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: task.pipeLineId}, function (dataset) {
                        var json = DyyBase.getSelect(dataset);
                        $segment.add(json).disable();
                    });

                    // 起止点
                    $begin.asset([{id:task.fromPointId,text:task.fromPointName,mile:task.fromPointMileage2}]).disable();
                    $end.asset([{id:task.toPointId,text:task.toPointName,mile:task.toPointMileage2}]).disable();

                    window.initData.id = task.repairTaskId;
                    window.initData.name = task.name;
                    window.initData.assemblyCompany = task.assemblyCompany;
                    window.initData.pipeLineId = task.pipeLineId;

                    window.initData.fromPointId = task.fromPointId;
                    window.initData.fromPointName = task.fromPointName;
                    window.initData.fromPointMileage = task.fromPointMileage2;

                    window.initData.toPointId = task.toPointId;
                    window.initData.toPointName = task.toPointName;
                    window.initData.toPointMileage = task.toPointMileage2;

                    window.initData.planStartDate = task.planStartDate;
                    window.initData.planCompletionDate = task.planCompletionDate;
                    window.initData.dataStated = task.dataState;
                }
                if(defect){
                    var list = defect.list;
                    if(list && list.length > 0){
                        var arr = [];
                        for(var i = 0, len = list.length; i < len; i++){
                            arr.push(list[i].id);
                            window.initData.defectInfoId.push(list[i].id);
                        }
                        filter.addChangeIds(arr);
                        //loadSelectDamage();
                    }
                }
            }
        });
    }
    /*** 编辑 - end ***/

    /*** 一键维修 - start ***/
    if(window.initData.defectInfoId.length){
        $pipeline.fit(window.initData.pipeLineId).disable();
        $segment.disable();

        // 起止点
        $begin.asset([{id:window.initData.fromPointId,text:window.initData.fromPointName,mile:window.initData.fromPointMileage}]).disable();
        $end.asset([{id:window.initData.toPointId,text:window.initData.toPointName,mile:window.initData.toPointMileage}]).disable();
    }
    /*** 一键维修 - end ***/

})(jQuery, window, document);